<?php

namespace App\Http\Controllers;

use App\Models\FriendLink;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\FriendLinkRequest;

class FriendLinkController extends CommonController
{
    /**
     *获取友情链接列表
     */
    public function getList()
    {
        $lists = FriendLink::orderBy('id', 'asc')->orderBy('id', 'asc')->paginate(10);

        return view('friendlink.list')->with('lists', $lists)->with("actions", $this->actions);
    }


    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = FriendLink::orderBy('id', 'asc');

        if (isset($data['name']) && $data['name'] != '') {
            $sql->where('name', 'like', '%' . trim($data['name']) . '%');
        }

        if (isset($data['url']) && $data['url'] != '') {
            $sql->where('url', 'like', '%' . trim($data['url']) . '%');
        }

        if (isset($data['status']) && $data['status'] != '') {
            $sql->where('status', $data['status']);
        }

        if (isset($data['category_code']) && $data['category_code'] != '') {
            $sql->where('category_code', $data['category_code']);
        }

        $lists = $sql->paginate(10);

        return view('friendlink.search')->with("lists", $lists)->with("name", $data['name'])
            ->with("url", $data['url'])->with("status", $data['status'])
            ->with("category_code", $data['category_code'])->with("actions", $this->actions);

    }


    /**
     *获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = FriendLink::where('id', $data['id'])->orderBy('id', 'asc')->first();

        return view('friendlink.edit')->with('list', $list);
    }

    /**
     * 执行页面编辑
     */
    public function postEdit(FriendLinkRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        FriendLink::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('编辑成功', true, url('friendlink/list'));
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        return view('friendlink.create');
    }

    /**
     * 执行创建页面
     */
    public function postCreate(FriendLinkRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        $result = FriendLink::create($data);

        if (is_object($result)) {
            return AjaxCallbackMessage('创建成功', true, url('friendlink/list'));
        } else {
            return AjaxCallbackMessage('创建失败', false);
        }
    }

    /**
     * 执行一个删除
     */
    public function postDelete(Request $request){
        $data = $request->input();
        FriendLink::where('id', $data['id'])->delete();

        return AjaxCallbackMessage('删除成功', true, url('recommend/list'));
    }

}
